package com.suli.multimedia.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scenic.util.ResponseResult;
import com.suli.multimedia.entity.Image;
import com.suli.multimedia.entity.Scenic;
import com.suli.multimedia.service.ImageService;
import com.suli.multimedia.service.ScenicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SuLi
 * @since 2022-03-29
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/scenic")
public class ScenicController {

    @Autowired
    ScenicService scenicService;
    @Autowired
    ImageService imageService;
    /**
     * 景点录入
     * @param scenic 景点实体类
     * @return 录入结果
     */
    @PostMapping("/insertScenic")
    public ResponseResult insertScenic(@RequestBody Scenic scenic){
        QueryWrapper<Scenic> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("scenic_name", scenic.getScenicName());
        Scenic scenic1 = scenicService.getOne(queryWrapper);
        if(!ObjectUtils.isEmpty(scenic1)){
            return   ResponseResult.error().message("景点已存在").data("scenicId","no");
        }
        if(scenicService.save(scenic))
            return   ResponseResult.ok().data("scenicId",scenic.getScenicId());
        return   ResponseResult.error();
    }

    /**
     * 分页查询景点
     * @param limit 查询数量
     * @param page 页码
     * @return 查询结果
     */
    @GetMapping("/getScenic/{page}/{limit}")
    public ResponseResult getScenic(@PathVariable Long limit, @PathVariable Long page){
        Map<String, Object> scenic = scenicService.getScenic(page, limit);
        if (!ObjectUtils.isEmpty(scenic)) {
            return ResponseResult.ok().data(scenic);
        }
        return ResponseResult.error();
    }

    /**
     * 分页获取景点带配图
     * @param limit 查询数量
     * @param page 页码
     * @return 查询结果
     */
    @GetMapping("/getScenicAndImage/{page}/{limit}")
    public ResponseResult getScenicAndImage(@PathVariable Long limit, @PathVariable Long page){
        Map<String, Object> scenic = scenicService.getScenicAndImage(page, limit);
        if (ObjectUtils.isEmpty(scenic)) {
            return ResponseResult.error();
        }
        return ResponseResult.ok().data(scenic);
    }

    /**
     * 逻辑删除景点
     * @param scenicIds 景点名称集合
     * @return 删除结果
     */
    @DeleteMapping("/deleteScenic")
    public ResponseResult deleteScenic(@RequestBody List<String> scenicIds){
       if(scenicService.removeByIds(scenicIds)){
           scenicIds.forEach(scenicId->{
               QueryWrapper<Image> queryWrapper=new QueryWrapper<>();
               queryWrapper.eq("scenic_id", scenicId);
               imageService.remove(queryWrapper);
           });
           return ResponseResult.ok().message("删除成功");
       }
        return ResponseResult.error().message("删除失败");
    }

    /**
     * 根据景点名称模糊查找景点
     * @param name 景点名称
     * @return 景点列表
     */
    @GetMapping("/getScenic/{name}")
    public ResponseResult searchByName(@PathVariable String name){

        Map<String, Object> map = scenicService.searchScenicByName(name);
        if(ObjectUtils.isEmpty(map)){
            return ResponseResult.error().message("未查到数据");
        }
        return ResponseResult.ok().data(map);
    }

    /**
     *模糊查询景点(带图片)
     * @param name 景点名称
     * @return 景点信息
     */
    @GetMapping("/searchScenicAndImage/{name}/{page}/{limit}")
    public ResponseResult searchScenicAndImage(@PathVariable String name,
                                               @PathVariable Long limit,
                                               @PathVariable Long page){
        Map<String, Object> map = scenicService.searchScenicAndImage(page,limit,name);
        if(ObjectUtils.isEmpty(map)){
            return ResponseResult.error().message("未查到数据");
        }
        return ResponseResult.ok().data(map);
    }

    /**
     * 根据景点类型分页获取景点
     * @param scenicType 景点类型
     * @param limit 页码
     * @param page 数量
     * @return 景点列表
     */
    @GetMapping("/searchScenicByType/{scenicType}/{page}/{limit}")
    public ResponseResult searchScenicByType(@PathVariable String scenicType,
                                               @PathVariable Long limit,
                                               @PathVariable Long page){
        Map<String, Object> map = scenicService.searchScenicImageBType(page,limit,scenicType);
        if(ObjectUtils.isEmpty(map)){
            return ResponseResult.error().message("未查到数据");
        }
        return ResponseResult.ok().data(map);
    }

    /**
     * 分页获取被逻辑删除的景点
     * @param limit 数量
     * @param page 页码
     * @return 文章列表
     */
    @GetMapping("/getScenicDeleted/{page}/{limit}")
    public ResponseResult searchDeleted(@PathVariable Long limit, @PathVariable Long page){
        Map<String, Object> scenicDeleted = scenicService.getScenicDeleted(page, limit);
     if(ObjectUtils.isEmpty(scenicDeleted)){
         return ResponseResult.error().message("未获取到文章");
     }
        return ResponseResult.ok().data(scenicDeleted);
    }

    /**
     * 批量还原景点
     * @param ids 景点id集合
     * @return 还原结果
     */
    @PutMapping("/recoverScenic")
    public ResponseResult recoverScenic(@RequestBody List<String> ids){
        if(scenicService.recovery(ids)){
            return ResponseResult.ok().message("还原成功");
        }
        return ResponseResult.error().message("还原失败");
    }

    /**
     * 永久删除景点
     * @param ids 景点id集合
     * @return 景点删除结果
     */
    @DeleteMapping("/deleteScenicDeleted")
    public ResponseResult deleteScenicDeleted(@RequestBody List<String> ids){
        scenicService.deleteScenic(ids);
            return ResponseResult.ok().message("永久删除成功");

    }

    /**
     * 获取景点信息
     * @param scenicId 景点id
     * @return 景点信息
     */
    @GetMapping("/getScenicInfo/{scenicId}")
    public ResponseResult getScenicInfo(@PathVariable Integer scenicId){
        Scenic scenic = scenicService.getById(scenicId);
        if(ObjectUtils.isEmpty(scenic))
        return ResponseResult.error().message("获取文章信息失败");
        return ResponseResult.ok().data("scenicInfo",scenic);
    }

    /**
     * 更新景点信息
     * @param scenic 景点实体类
     * @return 更新结果
     */
    @PutMapping("/updateScenic")
    public ResponseResult updateScenic(@RequestBody Scenic scenic){
        if(scenicService.updateById(scenic))
            return ResponseResult.ok();
            return ResponseResult.error().message("更新失败");
    }
}
